package com.zhaopin.qa.jmeter.plugins.jsonpathbatchassertion;

import org.apache.jmeter.config.Argument;

public class JSONPathAssertionArgument extends Argument {

	private static final long serialVersionUID = 241L;

	public static final String JSONPATH = "JSONPathAssertionArgument.JSON_PATH";
	public static final String EXPECTEDVALUE = "JSONPathAssertionArgument.EXPECTED_VALUE";
	public static final String JSONVALIDATION = "JSONPathAssertionArgument.JSONVALIDATION";
	public static final String EXPECT_NULL = "JSONPathAssertionArgument.EXPECT_NULL";
	public static final String INVERT = "JSONPathAssertionArgument.INVERT";
	public static final String ISREGEX = "JSONPathAssertionArgument.ISREGEX";

	public String getJsonPath() {
		return this.getPropertyAsString(JSONPATH);
	}

	public void setJsonPath(String jsonPath) {
		this.setName(jsonPath);
		this.setProperty(JSONPATH, jsonPath);
	}

	public String getExpectedValue() {
		return this.getPropertyAsString(EXPECTEDVALUE);
	}

	public void setExpectedValue(String expectedValue) {
		this.setProperty(EXPECTEDVALUE, expectedValue);
	}

	public void setJsonValidationBool(boolean jsonValidation) {
		this.setProperty(JSONVALIDATION, jsonValidation);
	}

	public void setExpectNull(boolean val) {
		this.setProperty(EXPECT_NULL, val);
	}

	public boolean isExpectNull() {
		return this.getPropertyAsBoolean(EXPECT_NULL);
	}

	public boolean isJsonValidationBool() {
		return this.getPropertyAsBoolean(JSONVALIDATION);
	}

	public void setInvert(boolean invert) {
		this.setProperty(INVERT, invert);
	}

	public boolean isInvert() {
		return this.getPropertyAsBoolean(INVERT);
	}

	public void setIsRegex(boolean flag) {
		this.setProperty(ISREGEX, flag);
	}

	public boolean isUseRegex() {
		return this.getPropertyAsBoolean(ISREGEX, true);
	}

	public JSONPathAssertionArgument() {
		this(null, null);
	}

	public JSONPathAssertionArgument(Argument arg) {
		this(arg.getName(), arg.getValue());
	}

	public JSONPathAssertionArgument(String jsonpath, String expected) {
		this(jsonpath, expected, true, false, false, false);
	}

	public JSONPathAssertionArgument(String jsonPath, String expectedValue, boolean jsonValidation, boolean isRegex,
			boolean expectNull, boolean invert) {
		this.setJsonPath(jsonPath);
		this.setExpectedValue(expectedValue);
		this.setJsonValidationBool(jsonValidation);
		this.setIsRegex(isRegex);
		this.setInvert(invert);
	}

	@Override
	public String toString() {
		String invert = this.isInvert() ? "not" : "";

		if (this.isExpectNull()) {// 断言空
			return "Assert :" + this.getJsonPath() + " is " + invert + " null";
		} else {
			if (this.isJsonValidationBool()) {// 断言值
				if (this.isUseRegex()) {// 断言匹配
					return "Assert: " + this.getJsonPath() + " " + invert + " matchs regex " + this.getExpectedValue();
				} else {// 断言等于
					return "Assert: " + this.getJsonPath() + " " + invert + " = " + this.getExpectedValue();
				}
			} else {
				// 断言存在
				return "Assert: " + this.getJsonPath() + " " + invert + " exist";
			}
		}
	}
}
